package io.lkt.modules.sys.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import io.lkt.common.util.Query;
import io.lkt.common.utils.DateUtil;
import io.lkt.common.validator.ValidatorUtils;
import io.lkt.modules.sys.entity.BArticleInfoEntity;
import io.lkt.modules.sys.service.BArticleInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.lkt.modules.sys.entity.BBeginnerArticleInfoEntity;
import io.lkt.modules.sys.service.BBeginnerArticleInfoService;
import io.lkt.common.utils.PageUtils;
import io.lkt.common.utils.R;



/**
 * 新手学堂文章信息
 *
 * @author xss
 * @email dustwas@163.com
 * @date 2021-12-15 15:16:02
 */
@RestController
@RequestMapping("/sys/bbeginnerarticleinfo")
public class BBeginnerArticleInfoController {
    @Autowired
    private BBeginnerArticleInfoService bBeginnerArticleInfoService;

    @Autowired
    private BArticleInfoService bArticleInfoService;

    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("sys:bbeginnerarticleinfo:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = bBeginnerArticleInfoService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/queryList")
    public R queryList(@RequestParam Map<String, Object> params){
        Query query = new Query(params);

        List<BBeginnerArticleInfoEntity> list = bBeginnerArticleInfoService.queryList(query);
        int total = bBeginnerArticleInfoService.queryListTotal(query);

        PageUtils page = new PageUtils(list, total, query.getLimit(), query.getPage());

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("sys:bbeginnerarticleinfo:info")
    public R info(@PathVariable("id") Integer id){
        BBeginnerArticleInfoEntity bBeginnerArticleInfo = bBeginnerArticleInfoService.getById(id);

        return R.ok().put("bBeginnerArticleInfo", bBeginnerArticleInfo);
    }


    /**
     * 信息
     */
    @RequestMapping("/getInfo/{id}")
    public R getInfo(@PathVariable("id") Integer id){
        BBeginnerArticleInfoEntity bBeginnerArticleInfo = bBeginnerArticleInfoService.getInfoById(id);

        return R.ok().put("bBeginnerArticleInfo", bBeginnerArticleInfo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("sys:bbeginnerarticleinfo:save")
    public R save(@RequestBody BBeginnerArticleInfoEntity bBeginnerArticleInfo){
        // 根据文章 ID 查看文章是否在文章库，如果不在，则设为 null
        BArticleInfoEntity articleInfo = bArticleInfoService.getById(bBeginnerArticleInfo.getArticleId());
        if (Objects.isNull(articleInfo)) {
            bBeginnerArticleInfo.setArticleId(-1);
        }

        bBeginnerArticleInfo.setCreateTime(DateUtil.getCurDate());
        bBeginnerArticleInfoService.save(bBeginnerArticleInfo);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("sys:bbeginnerarticleinfo:update")
    public R update(@RequestBody BBeginnerArticleInfoEntity bBeginnerArticleInfo){
        ValidatorUtils.validateEntity(bBeginnerArticleInfo);

        // 根据文章 ID 查看文章是否在文章库，如果不在，则设为 null
        BArticleInfoEntity articleInfo = bArticleInfoService.getById(bBeginnerArticleInfo.getArticleId());
        if (Objects.isNull(articleInfo)) {
            bBeginnerArticleInfo.setArticleId(Integer.valueOf(-1));
        }
        bBeginnerArticleInfo.setModifyTime(DateUtil.getCurDate());
        bBeginnerArticleInfoService.updateById(bBeginnerArticleInfo);
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("sys:bbeginnerarticleinfo:delete")
    public R delete(@RequestBody Integer[] ids){
        bBeginnerArticleInfoService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @RequestMapping("/baseInfo/{id}")
    public R beginnerArticleInfo(@PathVariable("id") Integer id) {
        BBeginnerArticleInfoEntity bBeginnerArticleInfo = bBeginnerArticleInfoService.getBeginnerArticleInfoById(id);

        return R.ok().put("bBeginnerArticleInfo", bBeginnerArticleInfo);
    }

    @RequestMapping("/platformArticleInfo")
    public R platformArticleInfo(@RequestParam String gzhCode, @RequestParam Integer articleTypeId, @RequestParam Integer articleId) {
        BBeginnerArticleInfoEntity bBeginnerArticleInfo = bBeginnerArticleInfoService.getPlatformArticleInfo(gzhCode, articleTypeId, articleId);

        return R.ok().put("bBeginnerArticleInfo", bBeginnerArticleInfo);
    }

}
